package org.javabb.action;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PictureServlet implements Servlet {
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String picName = request.getParameter("pic");
		response.setContentType("image/jpeg");
		response.setHeader("Content-Disposition", "attachment; filename=\"" + picName + "\"");
		response.setHeader("Cache-Control", "private");
		InputStream inputStream = new FileInputStream("/var/lib/openshift/5222e47be0b8cdaa06000057/app-root/data/upload/" + picName);
		OutputStream output = response.getOutputStream();
		byte[] buffer = new byte[8192];
		int bytesRead;
		while ((bytesRead = inputStream.read(buffer)) >= 0) {
			output.write(buffer, 0, bytesRead);
		}
		inputStream.close();
		output.close();
	}

	public void destroy() {
		
	}

	public ServletConfig getServletConfig() {
		return null;
	}

	public String getServletInfo() {
		return null;
	}

	public void init(ServletConfig arg0) throws ServletException {
	}
}
